﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>分组平铺</title>
     <script language="JavaScript" src="../../Comm/JScript.js" type="text/javascript" ></script>
    <script type="text/javascript" src="../../Scripts/bootstrap/js/jquery.min.js"></script>
    <script src="../../Scripts/QueryString.js" type="text/javascript"></script>
    <!-- 引用通用的js文件. -->
    <script type="text/javascript" src="../../Scripts/config.js"></script>
    <script type="text/javascript" src="../../Comm/Gener.js"></script>
    <link href="../../../DataUser/Style/ccbpm.css" rel="Stylesheet" />


    <script type="text/javascript">

        var selectedEles = null;
        var mapExt = null;

		function loadEntities(mapExt) {
			selectedEles = new Entities("BP.Sys.FrmEleDBs");
            selectedEles.Retrieve("FK_MapData", mapExt.FK_MapData, "EleID", mapExt.AttrOfOper, "RefPKVal", GetQueryString("PKVal"));
		}

		$(function () {

		    $("#Msg").html("正在加载,请稍后......");

		    var mypk = GetQueryString("FK_MapExt");
		    mapExt = new Entity("BP.Sys.MapExt", mypk);


		    var srcOfGroup = mapExt.Tag1; //分组.
		    var srcOfEntitis = mapExt.Tag2; //实体.

		    var groups = DBAccess.RunSQLReturnTable(srcOfGroup); //分组.
		    var entities = DBAccess.RunSQLReturnTable(srcOfEntitis); //数据源.

		    var html = "<table style='width:100%;' >";

		    var isRB = true;
		    if (mapExt.GetPara('SelectType') == 1)
		        isRB = false;

		    //获得已经选择的集合.
		    loadEntities(mapExt);

		    for (var i = 0; i < groups.length; i++) {
		        var group = groups[i];

		        html += "<tr>";
		        html += "<th colspan=3>";

		        if (isRB == true)
		            html += "" + group.Name;
		        else
		            html += "<input type='checkbox'  onclick='CheckAll(this.checked, \"" + group.No + "\");' id='Group_" + group.No + "'  /><label for='Group_" + group.No + "'>" + group.Name + "</label>";

		        html += "</th>";
		        html += "</tr>";


		        var myNum = -1;
		        for (var idx = 0; idx < entities.length; idx++) {

		            en = entities[idx];

		            var myidx = 0;
		            var oOfEn;
		            for (var obj in en) {
		                if (myidx == 2) {
		                    oOfEn = obj;
		                    break;
		                }
		                myidx++;
		            }

		            myidx = 0;
		            var oOfGroup;
		            for (var obj in en) {
		                if (myidx == 0) {
		                    oOfGroup = obj;
		                    break;
		                }
		                myidx++;
		            }

		            if (en[oOfEn] != group[oOfGroup])
		                continue;

		            var isHave = false;
		            for (var iEle = 0; iEle < selectedEles.length; iEle++) {

		                var ele = selectedEles[iEle];
		                if (ele == null || ele == undefined)
		                    continue;

		                if (ele.Tag1 == en.No) {
		                    isHave = true;
		                    break;
		                }
		            }

		            var lab = en.Name;
					var checked = "";
		            if (isHave) {
		                lab = "<font color=green>" + en.Name + "</font>";
						checked = "checked";
					}

		            myNum++;
		            if (myNum == 0)
		                html += "<TR>";

		            if (isRB == true)
		                html += "<td><input type='radio' name='RB' id='RB_" + en.No + "' value='" + en.No + "' " + checked + " /><label for='RB_" + en.No + "'>" + lab + "</label></td>";
		            else
		                html += "<td><input type='checkbox' name='" + group.No + "' id='CB_" + en.No + "' value='" + en.No + "' " + checked + " /><label for='CB_" + en.No + "'>" + lab + "</label></td>";

		            if (myNum == 2) {
		                html += "</tr>";
		                myNum = -1;
		            }
		        }

		        if (idx == 1) {
		            html += "<td colspan=2></td>";
		            html += "</tr>";
		        }

		        if (idx == 2) {
		            html += "<td></td>";
		            html += "</tr>";
		        }

		    }
		    html += "</table>";

		    $("#Msg").html("");
		    $("#Msg").html(html);
		});
       
        //选择与取消选择.
        function CheckAll(checked, no) {

            $("input[name='" + no + "']").each(function () {
                this.checked = checked;
            });
         
        }

        //保存.
        function Save() {

            //获得存储的值.
            var selectVals = [];

            //获得选择的数据.
            var selectType = mapExt.GetPara("SelectType");
            if (selectType == "0") {  //单选.
                selectVals.push($("input[name='RB']:checked").val());
            } else {

                var cbs = $("input[type='checkbox']:checked");

                cbs.each(function () {
                    if (this.id.indexOf('CB_') == -1)
                        return true;
                    selectVals.push(this.value);
                });
            }

            //删除变量中没有的数据,已经取消掉的.  selectedEles 数据.
            var FK_MapData = mapExt.FK_MapData;
            var keyOfEn = mapExt.AttrOfOper;
            var oid = GetQueryString("PKVal");
            //
            var insertArray = selectedElements(selectedEles, selectVals);
            $.each(insertArray, function (i, o) {
                var frmEleDB = new Entity("BP.Sys.FrmEleDB");
                frmEleDB.MyPK = keyOfEn + "_" + oid + "_" + o;
                frmEleDB.FK_MapData = FK_MapData;
                frmEleDB.EleID = keyOfEn;
                frmEleDB.RefPKVal = oid;
                frmEleDB.Tag1 = o;
                if (frmEleDB.Update() == 0) {
                    frmEleDB.Insert();
                }
            });

            var deleteArray = canceledElements(selectedEles, selectVals);
            $.each(deleteArray, function (i, o) {
                var frmEleDB = new Entity("BP.Sys.FrmEleDB");
                frmEleDB.MyPK = keyOfEn + "_" + oid + "_" + o;
                frmEleDB.Delete();
            });

            //loadEntities();

            //parent.CloseBootstrapDialog();
			return selectVals;

            //插入新选择的数据.
            //获得已经选择的集合.
            // var eleDBs = new Entities("BP.Sys.FrmEleDBs");
            // eleDBs.Retrieve("FK_MapData", mapExt.FK_MapExt, "EleID", mapExt.AttrOfOper, "RefPKVal", GetQueryString("PKVal"));
            //组成返回的字符串.
            //执行返回,并关闭窗口.
        }

		// 新增
		function selectedElements(entities, selectedArray) {
			var array = [];
			$.each(selectedArray, function (i, e) {
				if (typeof e === "undefined") {
					return true;
				}
				var exists = false;
				$.each(entities, function (n, o) {
					if (o.Tag1 == e) {
						exists = true;
						return false;
					}
				});
				if (!exists) {
					array.push(e);
				}
			});
			return array;
		}
		// 删除
		function canceledElements(entities, selectedArray) {
			if (!$.isArray(selectedArray)) {
				return;
			}
			var array = [];
			$.each(entities, function (i, o) {
				var value = o.Tag1;
				var t = selectedArray.filter(function (e) {
					return e == value;
				});
				if (t.length == 0) {
					array.push(value);
				}
			});
			return array;
		}
    </script>
</head>
<body>
 
 <table id="Table1" >
  
 </table>

 <input type=button id="Btn_Save" onclick="Save();" style="display: none;" value="Save"/>
 <div id="Msg"> </div>

</body>
</html>
